package com.tus.util;

import java.util.*;

public class MetadataParser {
    public static Map<String, String> parse(String metadataHeader) {
        Map<String, String> map = new HashMap<>();
        if (metadataHeader == null || metadataHeader.trim().isEmpty()) return map;

        for (String item : metadataHeader.split(",")) {
            item = item.trim();
            if (item.isEmpty()) continue;

            String[] parts = item.split(" ", 2);
            String key = parts[0];
            String value = (parts.length > 1) ? decode(parts[1]) : "";
            map.put(key, value);
        }
        return map;
    }

    private static String decode(String base64) {
        try {
            return new String(java.util.Base64.getDecoder().decode(base64));
        } catch (Exception e) {
            return base64;
        }
    }
}